package bosque.unc.gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

import java.awt.Color;

import javax.swing.UIManager;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.SwingConstants;

import bosque.unc.enumerations.EncoderNombre;
import bosque.unc.enumerations.EncoderRango;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.Toolkit;

public class EncodersGUI extends JFrame {
	
    private boolean flagMostrar = false; 	// flag para refrescar los valores de los labes
    private HiloActualizacionEncodersGUI updater;
    private static Scoba scoba;
    
	private JPanel contentPane;
	
	private JLabel lblAlfaGrados;
	private JLabel lblAlfaMin;
	private JLabel lblAlfaSeg;
	private JLabel lblDeltaGrados;
	private JLabel lblDeltaMin;
	private JLabel lblDeltaSeg;
	private JLabel lblAlfaByte1;
	private JLabel lblAlfaByte2;
	private JLabel lblAlfaByte3;
	private JLabel lblAlfaByte4;
	private JLabel lblDeltaByte1;
	private JLabel lblDeltaByte2;
	private JLabel lblDeltaByte3;
	private JLabel lblDeltaByte4;
	private JLabel labelDeltaSigno;
	private JLabel labelAlfaSigno;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				
				try
				{
					System.out.println("Look and Fell: " + UIManager.getSystemLookAndFeelClassName());
					UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
				}
				catch (Exception ex)
				{
					System.out.println("Failed loading L&F: ");
					System.out.println(ex);
				}
				
				try {
					EncodersGUI frame = new EncodersGUI(getScoba());
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public EncodersGUI(Scoba sco) 
	{
		this.setScoba(sco);
		setIconImage(Toolkit.getDefaultToolkit().getImage("D:\\WorkTesis\\Scoba\\src\\bosque\\unc\\gui\\resources\\encoder.png"));
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent arg0) {
				flagMostrar = false;
				
			}
		});
		Inicialize();
	}
	
	public void actualizarGui()
	{
		
			updater = new HiloActualizacionEncodersGUI(EncodersGUI.getScoba());
			updater.start();
		
	}
	
	public void Inicialize()
	{
		setFont(new Font("Arial", Font.PLAIN, 12));
		setTitle("Encoders");
		setResizable(false);
		
		setBounds(100, 100, 443, 278);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panelEncoderAlfa = new JPanel();
		panelEncoderAlfa.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Encoder Alfa", TitledBorder.LEFT, TitledBorder.TOP, null, new Color(255, 0, 0)));
		panelEncoderAlfa.setToolTipText("Valor del Angulo Horario");
		panelEncoderAlfa.setBounds(37, 11, 375, 77);
		contentPane.add(panelEncoderAlfa);
		panelEncoderAlfa.setLayout(null);
		
		JLabel lblAlfaDP = new JLabel(":");
		lblAlfaDP.setHorizontalAlignment(SwingConstants.CENTER);
		lblAlfaDP.setFont(new Font("Dialog", Font.PLAIN, 30));
		lblAlfaDP.setBounds(132, 21, 20, 33);
		panelEncoderAlfa.add(lblAlfaDP);
		
		JLabel lblAlfaDP2 = new JLabel(":");
		lblAlfaDP2.setHorizontalAlignment(SwingConstants.CENTER);
		lblAlfaDP2.setFont(new Font("Dialog", Font.PLAIN, 30));
		lblAlfaDP2.setBounds(255, 21, 20, 33);
		panelEncoderAlfa.add(lblAlfaDP2);
		
		lblAlfaGrados = new JLabel("000");
		lblAlfaGrados.setHorizontalAlignment(SwingConstants.CENTER);
		lblAlfaGrados.setFont(new Font("Dialog", Font.PLAIN, 30));
		lblAlfaGrados.setBounds(52, 15, 83, 45);
		panelEncoderAlfa.add(lblAlfaGrados);
		
		lblAlfaMin = new JLabel("1");
		lblAlfaMin.setHorizontalAlignment(SwingConstants.CENTER);
		lblAlfaMin.setFont(new Font("Dialog", Font.PLAIN, 30));
		lblAlfaMin.setBounds(162, 15, 83, 45);
		panelEncoderAlfa.add(lblAlfaMin);
		
		lblAlfaSeg = new JLabel("1");
		lblAlfaSeg.setHorizontalAlignment(SwingConstants.CENTER);
		lblAlfaSeg.setFont(new Font("Dialog", Font.PLAIN, 30));
		lblAlfaSeg.setBounds(285, 15, 83, 45);
		panelEncoderAlfa.add(lblAlfaSeg);
		
		labelAlfaSigno = new JLabel("-");
		labelAlfaSigno.setHorizontalAlignment(SwingConstants.CENTER);
		labelAlfaSigno.setFont(new Font("Dialog", Font.PLAIN, 30));
		labelAlfaSigno.setBounds(24, 21, 20, 33);
		panelEncoderAlfa.add(labelAlfaSigno);
		
		JPanel panelEncoderDelta = new JPanel();
		panelEncoderDelta.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Encoder Delta", TitledBorder.LEFT, TitledBorder.TOP, null, new Color(255, 0, 0)));
		panelEncoderDelta.setToolTipText("Valor de la Decliacion");
		panelEncoderDelta.setBounds(37, 99, 375, 77);
		contentPane.add(panelEncoderDelta);
		panelEncoderDelta.setLayout(null);
		
		JLabel lblDeltaDP = new JLabel(":");
		lblDeltaDP.setHorizontalAlignment(SwingConstants.CENTER);
		lblDeltaDP.setFont(new Font("Dialog", Font.PLAIN, 30));
		lblDeltaDP.setBounds(132, 21, 20, 33);
		panelEncoderDelta.add(lblDeltaDP);
		
		JLabel lblDeltaDP2 = new JLabel(":");
		lblDeltaDP2.setHorizontalAlignment(SwingConstants.CENTER);
		lblDeltaDP2.setFont(new Font("Dialog", Font.PLAIN, 30));
		lblDeltaDP2.setBounds(255, 21, 20, 33);
		panelEncoderDelta.add(lblDeltaDP2);
		
		lblDeltaGrados = new JLabel("1");
		lblDeltaGrados.setHorizontalAlignment(SwingConstants.CENTER);
		lblDeltaGrados.setFont(new Font("Dialog", Font.PLAIN, 30));
		lblDeltaGrados.setBounds(55, 15, 83, 45);
		panelEncoderDelta.add(lblDeltaGrados);
		
		lblDeltaMin = new JLabel("1");
		lblDeltaMin.setHorizontalAlignment(SwingConstants.CENTER);
		lblDeltaMin.setFont(new Font("Dialog", Font.PLAIN, 30));
		lblDeltaMin.setBounds(162, 15, 83, 45);
		panelEncoderDelta.add(lblDeltaMin);
		
		lblDeltaSeg = new JLabel("1");
		lblDeltaSeg.setHorizontalAlignment(SwingConstants.CENTER);
		lblDeltaSeg.setFont(new Font("Dialog", Font.PLAIN, 30));
		lblDeltaSeg.setBounds(285, 15, 83, 45);
		panelEncoderDelta.add(lblDeltaSeg);
		
		labelDeltaSigno = new JLabel("-");
		labelDeltaSigno.setHorizontalAlignment(SwingConstants.CENTER);
		labelDeltaSigno.setFont(new Font("Dialog", Font.PLAIN, 30));
		labelDeltaSigno.setBounds(25, 21, 20, 33);
		panelEncoderDelta.add(labelDeltaSigno);
		
		JLabel lblAlfaEnBina = new JLabel("Alfa en BCD :");
		lblAlfaEnBina.setHorizontalAlignment(SwingConstants.RIGHT);
		lblAlfaEnBina.setFont(new Font("Arial", Font.PLAIN, 15));
		lblAlfaEnBina.setBounds(45, 187, 119, 24);
		contentPane.add(lblAlfaEnBina);
		
		JLabel lblDeltaEnBinario = new JLabel("Delta en BCD :");
		lblDeltaEnBinario.setHorizontalAlignment(SwingConstants.RIGHT);
		lblDeltaEnBinario.setFont(new Font("Arial", Font.PLAIN, 15));
		lblDeltaEnBinario.setBounds(45, 220, 119, 24);
		contentPane.add(lblDeltaEnBinario);
		
		lblAlfaByte1 = new JLabel("1");
		lblAlfaByte1.setHorizontalAlignment(SwingConstants.CENTER);
		lblAlfaByte1.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblAlfaByte1.setBounds(194, 188, 47, 21);
		contentPane.add(lblAlfaByte1);
		
		lblAlfaByte2 = new JLabel("1");
		lblAlfaByte2.setHorizontalAlignment(SwingConstants.CENTER);
		lblAlfaByte2.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblAlfaByte2.setBounds(251, 188, 47, 21);
		contentPane.add(lblAlfaByte2);
		
		lblAlfaByte3 = new JLabel("1");
		lblAlfaByte3.setHorizontalAlignment(SwingConstants.CENTER);
		lblAlfaByte3.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblAlfaByte3.setBounds(308, 188, 47, 21);
		contentPane.add(lblAlfaByte3);
		
		lblAlfaByte4 = new JLabel("1");
		lblAlfaByte4.setHorizontalAlignment(SwingConstants.CENTER);
		lblAlfaByte4.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblAlfaByte4.setBounds(365, 188, 47, 21);
		contentPane.add(lblAlfaByte4);
		
		lblDeltaByte1 = new JLabel("1");
		lblDeltaByte1.setHorizontalAlignment(SwingConstants.CENTER);
		lblDeltaByte1.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblDeltaByte1.setBounds(194, 221, 47, 21);
		contentPane.add(lblDeltaByte1);
		
		lblDeltaByte2 = new JLabel("1");
		lblDeltaByte2.setHorizontalAlignment(SwingConstants.CENTER);
		lblDeltaByte2.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblDeltaByte2.setBounds(251, 221, 47, 21);
		contentPane.add(lblDeltaByte2);
		
		lblDeltaByte3 = new JLabel("1");
		lblDeltaByte3.setHorizontalAlignment(SwingConstants.CENTER);
		lblDeltaByte3.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblDeltaByte3.setBounds(308, 221, 47, 21);
		contentPane.add(lblDeltaByte3);
		
		lblDeltaByte4 = new JLabel("1");
		lblDeltaByte4.setHorizontalAlignment(SwingConstants.CENTER);
		lblDeltaByte4.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblDeltaByte4.setBounds(365, 221, 47, 21);
		contentPane.add(lblDeltaByte4);
	}

	
	/*Setters y Getters*/
	
	public JLabel getLblAlfaGrados() {
		return lblAlfaGrados;
	}

	public void setLblAlfaGrados(JLabel lblAlfaGrados) {
		this.lblAlfaGrados = lblAlfaGrados;
	}

	public JLabel getLblAlfaMin() {
		return lblAlfaMin;
	}

	public void setLblAlfaMin(JLabel lblAlfaMin) {
		this.lblAlfaMin = lblAlfaMin;
	}

	public JLabel getLblAlfaSeg() {
		return lblAlfaSeg;
	}

	public void setLblAlfaSeg(JLabel lblAlfaSeg) {
		this.lblAlfaSeg = lblAlfaSeg;
	}

	public JLabel getLblDeltaGrados() {
		return lblDeltaGrados;
	}

	public void setLblDeltaGrados(JLabel lblDeltaGrados) {
		this.lblDeltaGrados = lblDeltaGrados;
	}

	public JLabel getLblDeltaMin() {
		return lblDeltaMin;
	}

	public void setLblDeltaMin(JLabel lblDeltaMin) {
		this.lblDeltaMin = lblDeltaMin;
	}

	public JLabel getLblDeltaSeg() {
		return lblDeltaSeg;
	}

	public void setLblDeltaSeg(JLabel lblDeltaSeg) {
		this.lblDeltaSeg = lblDeltaSeg;
	}

	public JLabel getLblAlfaByte1() {
		return lblAlfaByte1;
	}

	public void setLblAlfaByte1(JLabel lblAlfaByte1) {
		this.lblAlfaByte1 = lblAlfaByte1;
	}

	public JLabel getLblAlfaByte2() {
		return lblAlfaByte2;
	}

	public void setLblAlfaByte2(JLabel lblAlfaByte2) {
		this.lblAlfaByte2 = lblAlfaByte2;
	}

	public JLabel getLblAlfaByte3() {
		return lblAlfaByte3;
	}

	public void setLblAlfaByte3(JLabel lblAlfaByte3) {
		this.lblAlfaByte3 = lblAlfaByte3;
	}

	public JLabel getLblAlfaByte4() {
		return lblAlfaByte4;
	}

	public void setLblAlfaByte4(JLabel lblAlfaByte4) {
		this.lblAlfaByte4 = lblAlfaByte4;
	}

	public JLabel getLblDeltaByte1() {
		return lblDeltaByte1;
	}

	public void setLblDeltaByte1(JLabel lblDeltaByte1) {
		this.lblDeltaByte1 = lblDeltaByte1;
	}

	public JLabel getLblDeltaByte2() {
		return lblDeltaByte2;
	}

	public void setLblDeltaByte2(JLabel lblDeltaByte2) {
		this.lblDeltaByte2 = lblDeltaByte2;
	}

	public JLabel getLblDeltaByte3() {
		return lblDeltaByte3;
	}

	public void setLblDeltaByte3(JLabel lblDeltaByte3) {
		this.lblDeltaByte3 = lblDeltaByte3;
	}

	public JLabel getLblDeltaByte4() {
		return lblDeltaByte4;
	}

	public void setLblDeltaByte4(JLabel lblDeltaByte4) {
		this.lblDeltaByte4 = lblDeltaByte4;
	}

	public boolean isFlagMostrar() {
		return flagMostrar;
	}

	public void setFlagMostrar(boolean flagMostrar) {
		this.flagMostrar = flagMostrar;
	}
	public JLabel getLabelDeltaSigno() {
		return labelDeltaSigno;
	}
	public JLabel getLabelAlfaSigno() {
		return labelAlfaSigno;
	}

	public void setLabelDeltaSigno(JLabel labelDeltaSigno) {
		this.labelDeltaSigno = labelDeltaSigno;
	}

	public void setLabelAlfaSigno(JLabel labelAlfaSigno) {
		this.labelAlfaSigno = labelAlfaSigno;
	}

	public static Scoba getScoba() {
		return scoba;
	}

	public static void setScoba(Scoba scoba) {
		EncodersGUI.scoba = scoba;
	}
	
	
	
	
}

class HiloActualizacionEncodersGUI extends Thread
{
	private Scoba scoba;
	private Integer delay = 100;
	
	public HiloActualizacionEncodersGUI(Scoba sco)
	{
		this.scoba = sco;
	}
	
	public void run()
	{
		while(this.scoba.getEncoderGUI().isFlagMostrar())
		{
			System.out.println("hilo actualizacion encoders activo");
			   /*Encoder Alfa*/
			   this.scoba.getEncoderGUI().getLblAlfaGrados().setText(String.format("%03d", Math.abs( this.scoba.getLecturas().getEncoder(EncoderNombre.ALFA).getGrados())));
			   this.scoba.getEncoderGUI().getLblAlfaMin().setText(String.format("%02d", Math.abs(this.scoba.getLecturas().getEncoder(EncoderNombre.ALFA).getMinutos())));
			   this.scoba.getEncoderGUI().getLblAlfaSeg().setText(String.format("%02d", Math.abs(this.scoba.getLecturas().getEncoder(EncoderNombre.ALFA).getSegundos())));
			   this.scoba.getEncoderGUI().getLabelAlfaSigno().setText( (this.scoba.getLecturas().getEncoder(EncoderNombre.ALFA).isSignoNegativo() ) ? "-": ""); 
			   			   
			   this.scoba.getEncoderGUI().getLblAlfaByte4().setText(String.format("%02X",(this.scoba.getLecturas().getEncoder(EncoderNombre.ALFA).getEncoderBits().get(EncoderRango.BCD_SEGUNDOS).byteValue())) );
 		       this.scoba.getEncoderGUI().getLblAlfaByte3().setText(String.format("%02X",(this.scoba.getLecturas().getEncoder(EncoderNombre.ALFA).getEncoderBits().get(EncoderRango.BCD_MINUTOS).byteValue())) );
			   this.scoba.getEncoderGUI().getLblAlfaByte2().setText(String.format("%02X",(this.scoba.getLecturas().getEncoder(EncoderNombre.ALFA).getEncoderBits().get(EncoderRango.BCD_DEC_GRAD).byteValue())) );
			   this.scoba.getEncoderGUI().getLblAlfaByte1().setText(String.format("%02X",(this.scoba.getLecturas().getEncoder(EncoderNombre.ALFA).getEncoderBits().get(EncoderRango.BCD_CENT_GRAD).byteValue())) );
							   
			   /*Encoder Delta*/
			   this.scoba.getEncoderGUI().getLblDeltaGrados().setText(String.format("%03d", Math.abs(this.scoba.getLecturas().getEncoder(EncoderNombre.DELTA).getGrados())));
			   this.scoba.getEncoderGUI().getLblDeltaMin().setText(String.format("%02d", Math.abs(this.scoba.getLecturas().getEncoder(EncoderNombre.DELTA).getMinutos())));
			   this.scoba.getEncoderGUI().getLblDeltaSeg().setText(String.format("%02d", Math.abs(this.scoba.getLecturas().getEncoder(EncoderNombre.DELTA).getSegundos())));
			   this.scoba.getEncoderGUI().getLabelDeltaSigno().setText( (this.scoba.getLecturas().getEncoder(EncoderNombre.DELTA).isSignoNegativo() ) ? "-": ""); 
			   
			   this.scoba.getEncoderGUI().getLblDeltaByte4().setText(String.format("%02X",(this.scoba.getLecturas().getEncoder(EncoderNombre.DELTA).getEncoderBits().get(EncoderRango.BCD_SEGUNDOS).byteValue())) );
 		       this.scoba.getEncoderGUI().getLblDeltaByte3().setText(String.format("%02X",(this.scoba.getLecturas().getEncoder(EncoderNombre.DELTA).getEncoderBits().get(EncoderRango.BCD_MINUTOS).byteValue())) );
			   this.scoba.getEncoderGUI().getLblDeltaByte2().setText(String.format("%02X",(this.scoba.getLecturas().getEncoder(EncoderNombre.DELTA).getEncoderBits().get(EncoderRango.BCD_DEC_GRAD).byteValue())) );
			   this.scoba.getEncoderGUI().getLblDeltaByte1().setText(String.format("%02X",(this.scoba.getLecturas().getEncoder(EncoderNombre.DELTA).getEncoderBits().get(EncoderRango.BCD_CENT_GRAD).byteValue())) );
			   
			   try
				{
					Thread.sleep(delay);
				} catch (InterruptedException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
	}

	public Integer getDelay() {
		return delay;
	}

	public void setDelay(Integer delay) {
		this.delay = delay;
	}
	
}
